package frontend.core;

import org.apache.log4j.Logger;
import org.apache.wicket.Session;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.request.Request;

import backend.model.other.User;
import backend.service.other.UserService;
import shared.util.MD5CryptographyUtil;

import java.util.Calendar;
import java.util.Locale;

public class TimeTrackingWebSession  extends AuthenticatedWebSession {
    private static Logger logger = Logger.getLogger(TimeTrackingWebSession.class);

	private static final long serialVersionUID = 1L;
	public static final String LOGIN_USERNAME_ATTR = "username";
	public static final String LOGIN_FULL_NAME_ATTR = "fullname";
	public static final String LOGIN_ROLE_ATTR = "role";

    private Calendar firstDayOfSelectedMonth;
    private Calendar selectedDay;

	public TimeTrackingWebSession(Request request) {
		super(request);
        setLocale(new Locale("RO"));
	}
	
	public static TimeTrackingWebSession getSession() {
	    return (TimeTrackingWebSession) Session.get();
	  }
	
	@Override
	public Roles getRoles() {
		Roles roles = new Roles();
		if (isSignedIn()) {
			String sessionRole = (String)getAttribute(LOGIN_ROLE_ATTR);
			roles.add(sessionRole);
		}
		return roles;
	}

	@Override
	public boolean authenticate(final String username, final String password) {
		UserService userService = TimeTrackingWebApplication.getService(UserService.class);
		User loginUser = userService.findUserByUsernameAndPassword(username, MD5CryptographyUtil.encrypt(password));
		if (loginUser != null) {
            logger.info("Logged in with user: " + username);
            logger.info("Logged in with role: " + loginUser.getRole());
			setAttribute(LOGIN_USERNAME_ATTR, username);
			setAttribute(LOGIN_FULL_NAME_ATTR, loginUser.getEmployee().getFirstName() + " " + loginUser.getEmployee().getLastName());
			setAttribute(LOGIN_ROLE_ATTR, loginUser.getRole().getKey());
			return true;
		}

        logger.info("Session attribute 'role' set to " + getAttribute(LOGIN_ROLE_ATTR));
		return false;
	}

    public Calendar getFirstDayOfSelectedMonth() {
        if (firstDayOfSelectedMonth == null) {
            firstDayOfSelectedMonth = Calendar.getInstance();
            firstDayOfSelectedMonth.setFirstDayOfWeek(Calendar.MONDAY);
        }
        return (Calendar) firstDayOfSelectedMonth.clone();
    }

    public Calendar getSelectedDay() {
        if (selectedDay == null) {
            selectedDay = Calendar.getInstance();
            selectedDay.setFirstDayOfWeek(Calendar.MONDAY);
        }
        return (Calendar) selectedDay.clone();
    }

    public void setFirstDayOfSelectedMonth(Calendar firstDayOfSelectedMonth) {
        this.firstDayOfSelectedMonth = firstDayOfSelectedMonth;
    }

    public void setSelectedDay(Calendar selectedDay) {
        this.selectedDay = selectedDay;
    }

}
